#!/bin/bash
cdate=$(date +%Y%m%d)
# 创建备份文件夹
mkdir -p /app/pmapp/bak/service/$cdate
# 创建docker-compose文件夹
mkdir -p /app/pmapp/scripts/privacy/system/
mkdir -p /app/pmapp/scripts/privacy/cmsGateway/
mkdir -p /app/pmapp/scripts/privacy/encipher/
mkdir -p /app/pmapp/scripts/privacy/job/

gatewayFileName="./docker-compose-gateway.yml"
jobFileName="./docker-compose-job.yml"
oriGatewayFileName="/app/pmapp/scripts/privacy/cmsGateway/docker-compose-gateway.yml"
oriJobFileName="/app/pmapp/scripts/privacy/job/docker-compose-job.yml"

# 加载镜像
find ./ -type f -name "*_$cdate" -exec docker load -i {} \;

sh /app/pmapp/bin/startDocker.sh

# 检查文件是否存在
if [ -e "$gatewayFileName" ]; then
  echo "文件 ${gatewayFileName} 存在,替换gateway服务"
  # 判断 docker-compose-gateway.yml 是否存在
  if [ -e "$oriGatewayFileName" ]; then
    echo "停止gateway服务"
    docker-compose -f /app/pmapp/scripts/privacy/cmsGateway/docker-compose-gateway.yml down
    echo "备份docker-compose-gateway.yml文件夹"
    mv /app/pmapp/scripts/privacy/cmsGateway/docker-compose-gateway.yml /app/pmapp/bak/service/$cdate/
  fi
  cp ./docker-compose-gateway.yml /app/pmapp/scripts/privacy/cmsGateway/
  echo "启动gateway服务"
  docker-compose -f /app/pmapp/scripts/privacy/cmsGateway/docker-compose-gateway.yml up -d
fi

# 检查文件是否存在
if [ -e "$jobFileName" ]; then
  echo "文件 ${jobFileName} 存在,替换job服务"
  # 判断 docker-compose-job.yml 是否存在
  if [ -e "$oriJobFileName" ]; then
    echo "停止job服务"
    docker-compose -f /app/pmapp/scripts/privacy/job/docker-compose-job.yml down
    echo "备份docker-compose-job.yml文件夹"
    mv /app/pmapp/scripts/privacy/job/docker-compose-job.yml /app/pmapp/bak/service/$cdate/
  fi
  cp ./docker-compose-job.yml /app/pmapp/scripts/privacy/job/
  echo "启动job服务"
  docker-compose -f /app/pmapp/scripts/privacy/job/docker-compose-job.yml up -d
fi



